<?php

namespace app\common\model;

use app\common\model\Base;
use think\Model;

class GoodsList extends Base
{

    // 设置当前模型对应的完整数据表名称
    protected $table = 'goods_list';

    /**
     * 获取列表
     */
    public static function getList($where = [], $field = '*', $order = '', $pageSize = "15")
    {
        $list = self::where($where)
            ->field($field)
            ->order($order)
            ->paginate(['list_rows' => $pageSize, 'query' => request()->param()]);
        $page = $list->render();
        $data['list'] = $list->toArray()['data'];
        $data['count'] = $list->total();
        $data['last_page'] = $list->toArray()['last_page'];
        $data['page'] = $page;
        return $data;
    }

    /**
     * 获取列表  不分页
     */
    public static function getAllList($where = [], $field = '*', $order = '', $limit = '0')
    {
        $data = self::where($where)
            ->field($field)
            ->order($order)
            ->limit($limit)
            ->select();
        return $data;
    }

    /**
     * 获取单条数据
     */
    public static function getInfo($where = [], $field = '*')
    {
        $data = self::where($where)
            ->field($field)
            ->find();
        return $data;
    }

    public function getShangTitleAttr($value, $data)
    {
        if (isset($data['shang_id'])) {
            $title = Shang::field('title')->where(['id' => $data['shang_id']])->value('title')??'无';
            return $title;
        } else {
            return '无';
        }
    }

    public function getShangInfoAttr($value, $data)
    {
        if (isset($data['shang_id'])) {
            $info = Shang::field('title,imgurl,color')->where(['id' => $data['shang_id']])->find()->toArray();
            $info['imgurl'] = imageUrl($info['imgurl']);
            return $info;
        } else {
            return '';
        }
    }

    public function getGoodsListAttr($value, $data)
    {
        $data = self::field('title,imgurl,price,real_pro,goods_type,gailv,reveal_gailv,stock')
            ->withAttr('imgurl', function ($value, $data) {
                return imageUrl($value);
            })
            ->where('goods_id', '=', $data['goods_id'])
            ->where('shang_id', '=', $data['shang_id'])
            ->order('shang_id asc')
            ->select()->toArray();
        return $data;
    }

    public function getGiveListAttr($value, $data)
    {
        $data = self::field('title,imgurl')
            ->withAttr('imgurl', function ($value, $data) {
                return imageUrl($value);
            })
            ->where('goods_id', '=', $data['goods_id'])
            ->where('give_money', '=', $data['give_money'])
            ->select()->toArray();
        return $data;
    }

}